<?php
class ent_navigationmenu extends Lead_EDO_Object_Abstract
{
    static $_tables = array('ent_navigate');
    
    
    public $linkurl;
    
    
    public function fetch($id)
    {
        parent::fetch($id);
        
        $this->linkurl = '/page/view/' . $this->id;
    }
    
    
    
   	function GetAppid ()
	{
		return Led_Application::getInstance()->getAppid();
	    
		/*
	    if ($enttypes[32]->config['multidimensional']) {
			$theappid = $appid;
		} else {
			$theappid = 0;
		}
		return $theappid;
		*/
	}


    
    public static function getBranch($langid, $parent, $specialorder = 0, $parsetree = TRUE, $parse_invisibility = 1, $parse_level = 1)
    {
		$theappid = self::getAppid();
		
		$coll = new Led_Entity_Collection($theappid, 32, $langid, 2, $parent, 1);
		
		
		if($parsetree)	
            $coll = self::parseTree($coll, $parse_invisibility, $parse_level);
		
		return $coll;
	}
	
	
	
	public static function ParseTree ($tree, $parse_invisibility = 1, $parse_level = 1)
	{
		$app = Led_Application::getInstance();
	    $navi = $app->getRoute();
		
	    $user = $app->getUser();	
	    
	    $unsets = array();
	    	
		if ($tree->count())
		{
			// set entities to be unset
			foreach ($tree as $key => $value) {
				
				if ($parse_level) {
					if ($value->minlevel > $user->level OR $value->maxlevel < $user->level) {
                        
					    $unsets[] = $key;
					}
				}
				
				if ($parse_invisibility) {
					if ($value->invisibility == 2) {
						$unsets[] = $key;
					}
					
					if ($navi) {
											    					    
					    if ($value->invisibility == 1 AND !in_array($value->id, $navi)) {
							 $unsets[] = $key;
						}
					}
				}
			
			}
			
			/*
			// unset them
			foreach ($treees as $key => $value) {
				if (isset($value->unset_me)) {
					unset ($tree->entities[$key]);
				}
			}*/
			
			foreach($unsets as $unset)
			 $tree->removeItem($unset);

			
		}
		return $tree;
	}
	
	
	public function getImages ()
	{
				
		$theappid = self::GetAppid();
		
		
		$navpic = new Led_Entity_Collection($theappid, 3, Led_Application::getInstance()->getLangid(), 2, $this->id, 2);
	
		if ($navpic->count()) {
			switch ($navpic->count()) {
			case 1:
			$image[0] = $image[1] = $image[2] = $navpic[0];
			break;
							
			case 2:
			$image[0] = $image[0] = $navpic[0];
			$image[1] = $navpic[1];
			break;
			
			case 3:
			$image[0] = $navpic[0];
			$image[1] = $navpic[1];
			$image[2] = $navpic[2];
			break;
			}
		} else {
			$image = NULL;
		}
		return $image;
	}


}
